#include"Sort.h"

void Swap(int* a, int* b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

void PrintArrary(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void BubbleSort(int* a, int n)
{
	for (int j = 0; j < n - 1; j++)
	{
		// 一趟数据
		for (int i = 0; i < n - 1 - j; i++)
		{
			if (a[i] > a[i + 1])
			{
				Swap(&a[i], &a[i + 1]);
			}
		}
	}
}

void InsertSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int temp = a[end + 1];
		while (end >= 0)
		{
			if (temp < a[end])
			{
				a[end + 1] = a[end];
				--end;
			}
			else
			{
				a[end + 1] = temp;
				break;
			}
		}
		a[end + 1] = temp;
	}
}

void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1) // 逐渐有序
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++) 
		{
			int end = i;
			int temp = a[end + gap];
			while (end >= 0)
			{
				if (temp < a[end])
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					a[end + gap] = temp;
					break;
				}
			}
			a[end + gap] = temp;
		}
	}
}

void SelectSort(int* a, int n)
{
	int begin = 0;
	int end = n - 1;
	while(begin < end)
	{
		int min = begin, max = begin;
		for (int i = begin + 1; i <= end; i++)// 控制下标，
		{									  //数据最小的下标放到前面
			if (a[min] > a[i])				 // 数据最大的下标放到最后
			{
				min = i;
			}

			if (a[max] < a[i])
			{
				max = i;
			}
		}

		Swap(&a[begin], &a[min]);
		if (max == begin) // 小坑，注意画图
		{
			max = min;
		}
		Swap(&a[end], &a[max]);
		++begin;
		--end;
	}
	
}

void AdjustDown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;// 算出父节点的孩节点的下标
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			++child;
		}

		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(int* a, int n)
{
	// 向下调整 直接建堆
	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
	{
		AdjustDown(a, n, i);
	}

	// 升序建大堆
	int end = n - 1;
	while (end >= 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}

// keyi 三数取中
//大小居中的值，也就是不是最大也不是最小的
int GetMid(int* a, int left, int right)
{
	int mid = (right - left) / 2;
	if (a[left] < a[mid])
	{
		if (a[mid] < a[right])
		{
			return mid;
		}
		else if (a[left] > a[right])
		{
			return left;
		}
		else
		{
			return right;
		}
	}
	else // a[left] > a[mid]
	{
		if (a[right] > a[left])
		{
			return left;
		}
		else if (a[right] < a[mid])
		{
			return mid;
		}
		else
		{
			return right;
		}
	}
}

// 非递归 -- hoare
void QuickSort(int* a, int left, int right)
{
	if (left >= right)
		return;

	// 小区间选择走插入， 可以减少90%左右的递归
	if (right - left + 1 < 10)
	{
		InsertSort(a + left, right - left + 1);
	}
	else
	{
		int begin = left;
		int end = right;

		//// 随机数做keyi
		//int randi = rand() % (right - left + 1);
		//randi += left;
		//Swap(&a[left], &a[randi]);


		////三数取中
		//int mid = GetMid(a, left, right);
		//Swap(&a[left], &a[mid]);

		int keyi = left;
		
		while (left < right)
		{
			// 找小
			// left < right 防止right越界
			while (left < right && a[right] >= a[keyi])
			{
				--right;
			}

			// 找大
			while (left < right && a[left] <= a[keyi])
			{
				++left;
			}
			// 各自找到就进行交换
			Swap(&a[left], &a[right]);
		}
		// 相遇
		Swap(&a[keyi], &a[left]);
		keyi = left;

		// [begin, keyi - 1] keyi [ keyi + 1, end]
		QuickSort(a, begin, keyi - 1);
		QuickSort(a, keyi + 1, end);
	}
}

// 递归 -- 指针法
void QuickSort1(int* a, int left, int right)
{
	if (left >= right)
		return;

	int keyi = left;
	int prev = left;
	int cur = left + 1;

	while (cur <= right)
	{
		if (a[cur] < a[keyi])
		{
			++prev;
			Swap(&a[prev], &a[cur]);
			++cur;
		}
		else
		{
			++cur;
		}
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;
	// [left, keyi - 1] keyi [ keyi + 1, right]
	QuickSort1(a, left, keyi - 1);
	QuickSort1(a, keyi + 1, right);
}

#include"Stack.h"
// 非递归 -- 栈
void QuickSortNonR(int* a, int left, int right)
{
	ST st;
	STInit(&st);
	STPush(&st, right);
	STPush(&st, left);

	while (!STEmpty(&st))
	{
		int begin = STTop(&st);
		STPop(&st);
		int end = STTop(&st);
		STPop(&st);

		// 走单趟
		int keyi = begin;
		int prev = begin;
		int cur = begin + 1;

		while (cur <= end)
		{
			if (a[cur] < a[keyi] && ++prev != cur)
				Swap(&a[prev], &a[cur]);

			++cur;
		}
		Swap(&a[keyi], &a[prev]);
		keyi = prev;

		// [begin, keyi - 1] keyi [ keyi + 1, end]
		if (keyi + 1 < end)
		{
			STPush(&st, end);
			STPush(&st, keyi + 1);
		}

		if (begin < keyi - 1)
		{
			STPush(&st, keyi - 1);
			STPush(&st, begin);
		}
	}
	STDestroy(&st);
}

void _MergeSort(int* a, int begin, int end, int* temp)
{
	int mid = (begin + end) / 2;
	_MergeSort(a, begin, mid, temp);
	_MergeSort(a, mid + 1, end, temp);

	// 归并
	int begin1 = begin, end1 = begin1 + mid;
	int begin2 = end1 + 1, end2 = end;

}

// 递归 
void MergeSort(int* a, int n)
{
	int* temp = (int*)malloc(sizeof(int) * n);
	if (temp == NULL)
	{
		perror("malloc fail");
		return;
	}
	_MergeSort(a, 0, n - 1, temp);

	free(temp);
	temp = NULL;
}